package com.share.service.sysmgr.privilege;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.share.bean.sysmgr.privilege.PrivilegeResource;
import com.share.bean.sysmgr.privilege.PrivilegeResourceItem;
import com.share.dao.sysmgr.privilege.PrivilegeResourceDao;


@Component("privilegeResourceManager")
public class PrivilegeResourceManagerImpl implements PrivilegeResourceManager{
	
	private PrivilegeResourceDao resDao ;

	@Resource
	public void setResDao(PrivilegeResourceDao resDao) {
		this.resDao = resDao;
	}

	@Override
	public void deleteResourceManager(int id) throws Exception {
		
		this.resDao.deleteResourceDao(id) ;
	}

	@Override
	public PrivilegeResource queryByIdResourceManager(int id) throws Exception {
		
		return this.resDao.queryByIdResourceDao(id) ;
	}

	@Override
	public List<PrivilegeResource> queryResourceManager() throws Exception {
		String hql = "select o from PrivilegeResource o" ;
		
		return this.resDao.queryResourceDao(hql) ;
	}

	@Override
	public void saveResourceManager(PrivilegeResource res) throws Exception {
		PrivilegeResourceItem resItem = new PrivilegeResourceItem() ;
		resItem.setId(res.getResId()) ;
		
		res.setResItem(resItem) ;
		this.resDao.saveResourceDao(res) ;
	}
	

	@Override
	public void updateResourceManager(PrivilegeResource res) throws Exception {
		
		this.resDao.updateResourceDao(res) ;
	}

	@Override
	public void saveResourceManager(int resItemId, String resData) throws Exception {
		PrivilegeResourceItem resItem = new PrivilegeResourceItem() ;
		resItem.setId(resItemId) ;
		
		String[] resspl = resData.split(",") ;
		
		if(null != resspl && resspl.length > 0) {
			for(int i=0; i<resspl.length; i++) {
				PrivilegeResource pr = new PrivilegeResource() ;
				pr.setResName(resspl[i].split("#")[0]) ;
				pr.setResUrl(resspl[i].split("#")[1]) ;
				pr.setResItem(resItem) ;
				this.resDao.saveResourceDao(pr) ;
			}
		}
	}

}
